package com.emep.changzhi.analyse.http.basics;

import com.emep.changzhi.analyse.entity.basics.SysIndustryTypeInfo;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.basics.SysIndustryTypeService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import com.emep.changzhi.analyse.utils.MapToObject;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author xy
 * @Description: 行业类型控制器
 * @date 2018/2/26 9:50
 * @version V1.0
 */
@RestController
@RequestMapping("industry")
@Api(value="行业类型controller",tags={"行业类型操作接口"})
public class SysIndustryTypeController {
    @Autowired
    private SysIndustryTypeService industryTypeService;
    MapToObject<SysIndustryTypeInfo> mapToObject=new MapToObject<>();

    @ApiOperation(value = "查询行业列表", notes = "传入若干参数")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean list(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex", "pageSize", "condition");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            int pageIndex = Integer.parseInt(payload.get("pageIndex").toString());
            int pageSize = Integer.parseInt(payload.get("pageSize").toString());
            Map<String, Object> condition = (Map<String, Object>) payload.get("condition");
            List<SysIndustryTypeInfo> industryList = industryTypeService.find(pageIndex, pageSize, condition);
            if(industryList != null){
                return result.boolAdd(industryList);
            }else {
                return result.error("未查到行业列表");
            }
        }
        return result;
    }

    @ApiOperation(value = "查询行业详情", notes = "传入若干参数")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean info(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() ==Constant.RESULT_CODE_SUCCESS) {
            String id = payload.get("id").toString();
            SysIndustryTypeInfo industry = industryTypeService.getIndustry(id);
            if (industry != null) {
                return result.boolAdd(industry);
            } else {
                return result.error("找不到该行业");
            }
        }
        return result;
    }

    @ApiOperation(value = "添加行业信息", notes = "传入若干参数")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean add(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "industry");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Map<String, Object> map = (Map<String, Object>) payload.get("industry");
            SysIndustryTypeInfo sysIndustryTypeInfo = new SysIndustryTypeInfo();
            boolean b= mapToObject.convertEntity(sysIndustryTypeInfo, map);
            if(b) {
                boolean a = industryTypeService.add(sysIndustryTypeInfo);
                if (a ) {
                    return result.boolAdd("新增失败");
                } else {
                    return result.error("该名称已存在");
                }
            }else {
                return result.error("参数错误");
            }
        }
        return result;
    }

    @ApiOperation(value = "更新行业信息", notes = "传入若干参数")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean update(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "industry");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            Map<String, Object> map = (Map<String, Object>) payload.get("industry");
            String id = map.get("id").toString();
            if(id !=null) {
                SysIndustryTypeInfo industry = industryTypeService.getIndustry(id);
                if (industry != null) {
                    boolean b = mapToObject.convertEntity(industry, map);
                    if (b) {
                        boolean a = industryTypeService.updateIndustry(industry);
                        if (a) {
                            return result.boolAdd("更新成功");
                        } else {
                            return result.error("该名称已存在");
                        }
                    } else {
                        return result.error("无此行业");
                    }
                } else {
                    return result.error("参数错误");
                }
            }else {
                return result.error("id为必填参数");
            }

        }
        return result;
    }

    @ApiOperation(value = "删除行业信息", notes = "传入若干参数")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultBean delete(@RequestBody Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "industryId");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            String industryId = payload.get("industryId").toString();
            if(industryId!=null) {
                SysIndustryTypeInfo sysIndustryTypeInfo = industryTypeService.getIndustry(industryId);
                if (sysIndustryTypeInfo != null) {
                    if(!sysIndustryTypeInfo.getIsDelete().equals(Constant.RESULT_DELETECODE_YES)){
                        sysIndustryTypeInfo.setIsDelete(Constant.RESULT_DELETECODE_YES);
                        boolean a = industryTypeService.deleteIndustry(sysIndustryTypeInfo);
                        if (a) {
                            return result.boolAdd("删除成功");
                        } else {
                            return result.boolAdd("删除失败");
                        }
                    }else {
                        return result.error("此行业状态为已删除,删除失败");
                    }
                } else {
                    return result.boolAdd("无此行业");
                }
            }else {
                return result.error("id为必填参数");
            }
        }
        return result;
    }
}
